// pages/authorization/authorization.js
let conf = require('../../app.config').app
Page({

  /**
   * 页面的初始数据
   */
  data: {
    model:false,
    isCheck: false,
    cdn:conf.cdnPath,
    act:'', //act为1 为普通注册渠道 不用到答题结果页
    scanTabHeight:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      alert: this.selectComponent("#alert"),
      act: options.act ? options.act : ''
    });

    this.getSystem();

    console.log(options.act,' - - - - - -789456123')
  },
  getSystem(){
    let _this = this;
    wx.getSystemInfo({
      success: function(res) {
        var w = res.windowWidth;
        var h = res.windowHeight;
        var scanTabHeight = w * (1443/750);
        _this.setData({
          scanTabHeight:scanTabHeight,
        });
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 切换隐私条约
  onprivacy(){
    let that = this
    /* 统计监测 */
    that.setData({
      model:!that.data.model
    })
  },
  // 复选框状态
  checkboxChange(e) {
    this.setData({ isCheck: !!e.detail.value[0] })
    console.log(e)
  },
  bindmobile(){
    let that = this
    /* 统计监测 */
    if (!this.data.isCheck) {
      that.data.alert.show({ content: '请同意使用条款和隐私政策' });
      // wx.showModal({
      //   title: '请同意使用条款和隐私政策',
      // })
    }
  },
  //手机号授权
  getPhoneNumber(e) {
    let that = this
    let { errMsg, encryptedData, iv } = e.detail
    if (errMsg == "getPhoneNumber:ok") {
      let parm = { mobileEncryptedData: encryptedData, mobileIv: iv, sourceStr: 'crm' }
      let friendOntrialOrderId = wx.getStorageSync('friendOntrialOrderId') || ''
      if (friendOntrialOrderId) {
        parm.friendOntrialOrderId = friendOntrialOrderId
      } else {
        parm.helpData = {
          customerId: wx.getStorageSync('shareopenid') || '',
          helpType: 'NEWUSER'
        }
      }
      console.log('授权参数parm', parm);
      /*
      if(wx.getStorageSync('share_openid')){
        parm.shareOpenid = wx.getStorageSync('share_openid');
      }

       */
        this.api.interface.authPhone(parm).then(res=>{
          wx.removeStorageSync('friendOntrialOrderId')
          console.log(res)
          if(res.data.code == 200){
            // wx.showModal({
            //   title: '手机号授权成功',
            // })

            if (!wx.getStorageSync('isGetCouponOk')) {
              let isGetCouponOk = (res.data.data.authExchangeData) ? true : false
              wx.setStorageSync('isGetCouponOk', isGetCouponOk)
            }
            wx.navigateTo({
              url: '../headauthorized/headauthorized?act=' + this.data.act,
            })

          }else {
            wx.showModal({
              title: res.data.message,
            })
          }
        })
    } else
    wx.showToast({ title: '请同意手机号码授权', icon: 'none', duration: 2000 })
  },
  ongoback(){
    wx.reLaunch({
      url: '../home/home',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})